package org.gavin.core.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.gavin.core.annotation.Values;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author grl
 * @date 2022/4/8
 * Created by admin on 2022/4/8 12:50
 */
public class ValuesValidator implements ConstraintValidator<Values, Object> {


    private List<String> values;

    @Override
    public void initialize(Values constraintAnnotation) {
        values = new ArrayList<>();
        String[] value = constraintAnnotation.value();
        values.addAll(Arrays.asList(value));
    }

    @Override
    public boolean isValid(Object inputValue, ConstraintValidatorContext constraintValidatorContext) {
        if(inputValue == null){
            return true;
        }
        return values.contains(String.valueOf(inputValue));
    }
}
